/**
 * 日志Controller
 * Created by hjqi on 2016/5/19.
 */
module.controller('InstanceLogsController',
    function ($scope,$rootScope,$state,$location, $stateParams,k8sService ){

        //inherit from ServiceDetailController
        // $scope.init = function(idx){
        //     _setLoadDataEvt(idx);
        // };

        //初始化事件监听
        //   通过$scope.svc对象获得集群信息
        // function _setLoadDataEvt(idx){

            $scope.$on('getLogEvt',function(event,item){
              $scope.loadonce = false;
              if(!$scope.loadonce) {
                    $scope.loadonce = true;
                    $scope.replicasname = item.metadata.name;
                    k8sService.getPodLog(item.metadata.name,$scope.svc.dbJson.node.clusterId).then(function(response){
                        $scope.tabs.lines = response.data.split("\n");
                        $scope.spanHide = false;
                    },function (response) {
                        var status  = response.status || "" ;
                        status+="" ;
                        if( !response || !response.data ){
                            response = {
                                data : "Request timeout!"
                            }
                        }else if( status.indexOf("4") !== -1 ){
                            response = {
                                data : "No logs!"
                            }
                        }
                        //if error , allow to reload
                        $scope.loadonce = false;
                        console.log("Get bad response from remote:"+JSON.stringify( response)  ) ;
                        alertify.error(response.data);
                    });
                }
            });
        }

    // }
);